package com.hwc.demo.mbs.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hwc.demo.mbs.bean.StudentReq;
import com.hwc.demo.mbs.bean.entity.Student;
import com.hwc.demo.mbs.consts.ErrorEnum;
import com.hwc.demo.mbs.repository.StudentRepository;
import com.hwc.demo.mbs.service.StudentService;
import com.hwc.one4all.basebean.BaseResult;

@RestController
public class StudentCtrller {
	@Autowired
	StudentRepository stuRepo;
	@Autowired
	StudentService stuService;
	
	@GetMapping("/stu")
	public BaseResult getAll() {
		
		return BaseResult.successData(stuRepo.findAll());
	}
	
	//综合分页查询
	@GetMapping("/stu/page")
	public BaseResult listPage(StudentReq stuReq) {
		Student entity = new Student();
		BeanUtils.copyProperties(stuReq, entity);
		
		//注意 JPA 的页码从0开始
		Page<Student> pager = stuService.findPage(stuReq.getPageNum()-1, stuReq.getPageSize(), entity);
		
		return BaseResult.successData(pager);
	}
	
	@GetMapping("/stu/test")
	public BaseResult test() {
		return BaseResult.failErrorEnum(ErrorEnum.MANAGE_UNLOGIN);
	}
}
